package pe.edu.upc.e41a.javasoft.daoImpl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import pe.edu.upc.e41a.javasoft.dao.TransaccionDao;
import pe.edu.upc.e41a.javasoft.domain.BusquedaTransaccion;
import pe.edu.upc.e41a.javasoft.domain.Publicacion;
import pe.edu.upc.e41a.javasoft.domain.Transaccion;

@Repository(value = "TransaccionDao")
public class TransaccionDaoImpl implements TransaccionDao {
	
	private static final Log log = LogFactory.getLog(TransaccionDaoImpl.class);

	@PersistenceContext
	transient EntityManager em;

	public final EntityManager entityManager() {
		EntityManager entityManager = em;
		if (entityManager == null)
			throw new IllegalStateException(
					"Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
		return entityManager;
	}	

	@Transactional
	@Override
	public void registrarCompra(Transaccion transaccion) {
		if (em == null)
			this.em = this.em;
		em.persist(transaccion);
	}

	@Override
	public void updateTransaccion(Transaccion transaccion) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteTransaccion(Transaccion transaccion) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public List<Transaccion> buscarVentasPorUsuario(Integer usuarioId){
		if (usuarioId == 0)
			throw new IllegalArgumentException(
					"The descripcion argument is required");
		
		List<Transaccion> mTransaccionList = null;
	   	
	   	StringBuilder mQuery = new StringBuilder();
	   	mQuery.append(" select t ");
	   	mQuery.append(" from Transaccion t ");
	   	mQuery.append(" inner join t.publicacion p ");
	   	mQuery.append(String.format(" WHERE p.usuario.codigo = %s ",usuarioId));
	   	mQuery.append(" order by t.fechaRegistro desc ");
	   	
	   	mTransaccionList = (List<Transaccion>) entityManager().createQuery(mQuery.toString()).getResultList();
		return mTransaccionList;				
		
		/*List<BusquedaTransaccion> mBusquedaTransaccionList = null;
	   	
	   	StringBuilder mQuery = new StringBuilder();
	   	mQuery.append(" select p.codigo, p.titulo, p.precio, t.codigo, t.cantidad, t.comentario, t.fecha_registro ");
	   	mQuery.append(" from publicacion p ");
	   	mQuery.append(" inner join p.transaccions t ");
	   	mQuery.append(String.format(" WHERE p.usuario.codigo = %s ",usuarioId));
	   	mQuery.append(" order by t.fecha_registro desc ");
	   	
	   	mBusquedaTransaccionList = (List<BusquedaTransaccion>) entityManager().createQuery(mQuery.toString()).getResultList();
		return mBusquedaTransaccionList;	*/		
		

	}
	
	@Override
	public List<Transaccion> buscarComprasPorUsuario(Integer usuarioId){
		if (usuarioId == 0)
			throw new IllegalArgumentException(
					"The descripcion argument is required");

		List<Transaccion> mTransaccionList = null;
	   	
	   	StringBuilder mQuery = new StringBuilder();
	   	mQuery.append(" select t ");
	   	mQuery.append(" from Transaccion t ");
	   	mQuery.append(" inner join t.publicacion p ");
	   	mQuery.append(String.format(" WHERE t.usuario.codigo = %s ",usuarioId));
	   	mQuery.append(" order by t.fechaRegistro desc ");
	   	
	   	mTransaccionList = (List<Transaccion>) entityManager().createQuery(mQuery.toString()).getResultList();
		return mTransaccionList;			
		
		/*TypedQuery<BusquedaTransaccion> q = em
				.createQuery(
						"select p.codigo, p.titulo, p.precio, t.codigo, t.cantidad, t.comentario, t.fecha_registro " +
						"from publicacion p " +
						"inner join p.transaccions t  " +		
						/*"inner join transaccion t on t.publicacion = p.codigo " +*
						"where t.usuario = :usuarioId " +
						"order by t.fecha_registro desc ",
						BusquedaTransaccion.class);
		q.setParameter("usuarioId", usuarioId);
		return q.getResultList();*/	
	}

	@Override
	public List<Transaccion> findAllTransaccions() {
		return em.createQuery("SELECT o FROM Transaccion o", Transaccion.class).getResultList();
	}



}
